//
//  CategoryGridItemView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 23.03.25.
//

import SwiftUI

struct CategoryGridItemView: View {
    var category:AccountingCategory?
    var isSelected = false
    @State var isDisable = false
    var gridItemBg: GridItemBg = .blueGrey
    
    var name: String
    var icon: String
    
    init(category: AccountingCategory, isSelected: Bool = false, gridItemBg: GridItemBg = .blueGrey) {
        self.init(name: category.name.orEmpty, icon: category.getIconAssetUri(), isSelected: isSelected, gridItemBg: gridItemBg)
        self.category = category 
    }
    
    init(categoryIcon: CategoryIcon, isSelected:Bool = false) {
        self.init(name: categoryIcon.name, icon: categoryIcon.getIconAsset(), isSelected: isSelected)
    }
    
    init(name: String , icon: String, isSelected: Bool = false , gridItemBg: GridItemBg = .blueGrey) {
        self.name = name
        self.icon = icon
        self.isSelected = isSelected
        self.gridItemBg = gridItemBg
    }
    
    var body: some View {
        VStack (spacing: 0) {
            ZStack {
                switch gridItemBg {
                case .blueGrey:
                    Image(isSelected ? "koi_ic_category_grid_bg_selected" : "koi_ic_category_grid_bg")
                case .white:
                    Image("koi_ic_category_item_bg")
                }
                if (category == .addSubItem)  {
                    Image("koi_ic_add").resizable().frame(width: 14, height: 14)
                } else if (category == .settingItem)  {
                    Image("koi_ic_setting").resizable().frame(width: 24, height: 24)
                } else {
                    Image(icon).resizable().frame(width: 24, height: 24)
                }
                
            }.frame(width: 48, height: 48)
            if (isSelected) {
                Text(name)
                    .bold()
                    .font(.system(size: 12))
                    .foregroundColor(Color.koiColorTitle)
                    .padding(.top, 2)
            } else if isDisable {
                Text(name)
                    .font(.system(size: 12))
                    .foregroundColor(Color.koiColorTitleSecond)
                    .padding(.top, 2)
            } else {
                Text(name)
                    .font(.system(size: 12))
                    .foregroundColor(Color.koiColorTitle)
                    .padding(.top, 2)
            }
            
        }.onAppear {
            isDisable = category?.isDisable ?? false
        }
    }
}

enum GridItemBg {
    case white, blueGrey
}
#Preview {
    CategoryGridItemView(category:AccountingCategory(name:"餐饮",icon:"linearicon://rice"))
}
